package com.hung.view.page;

import com.hung.pojo.Account;
import com.hung.pojo.Comment;
import com.hung.service.serviceImpl.CommentServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 评论弹出对话框
 *
 * @author Hung
 */
public class CommentDialog extends JDialog {
    final int WIDTH = 400;
    final int HEIGHT = 300;

    public CommentDialog(JFrame jf, String title, boolean isModel, Account account, int mid) {
        super(jf, title, isModel);
        this.setBounds(760, 390, WIDTH, HEIGHT);

        //组装视图
        Box box = Box.createVerticalBox();

        //创建内容框
        Box vBox = Box.createHorizontalBox();
        JLabel vLabel = new JLabel("尽所欲言:");
        vBox.add(vLabel);
        JTextArea textArea = new JTextArea(10, 20);
        vBox.add(textArea);
        box.add(vBox);
        box.add(Box.createVerticalStrut(30));

        JPanel btnPanel = new JPanel();
        JButton postBtn = new JButton("发表评论");
        postBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String text = textArea.getText();

                //判断内容是否为空，为空则提示
                if ("".equals(text) || text == null) {
                    JOptionPane.showMessageDialog(jf, "说出你的评论哦");
                    return;
                }

                //组成事件发送
                Comment comment = new Comment();
                comment.setComment(text);
                comment.setUid(account.getId());
                comment.setMid(mid);

                boolean b = new CommentServiceImpl().addComment(comment);
                if (b) {
                    JOptionPane.showMessageDialog(jf, "评论成功");
                    //TODO 将添加数据窗口关闭
                } else {
                    JOptionPane.showMessageDialog(jf, "评论失败，请重新再试");
                }
            }
        });

        //组装按钮
        btnPanel.add(postBtn);
        box.add(btnPanel);
        this.add(box);
    }
}
